<?php
ini_set('session.gc_maxlifetime', "36000");//10小时不过期
class Common{
    static $host="127.0.0.1";
    static $user="root";
    static $password="";
    static $database="lunwen";
    static $port="3306";
    static $mysqliObj=null;
    static $is_check_login=true;
    //第一个是msg 第二个是 是否成功
    public function returnJson($obj,$bool=false){
        $rt['status']=$bool;
        $rt['msg']=$obj;
        echo json_encode($rt);die();
    }
    public function startMysql(){
        if (self::$mysqliObj==null){
            self::$mysqliObj=mysqli_connect(self::$host,self::$user,self::$password,self::$database,self::$port);
        }
        return self::$mysqliObj;
    }
    public function __construct()
    {
        $this->session_start();
        $this->checkLogin();
        $this->startMysql();
    }
    public function sqlSafeString($string){
        return htmlspecialchars(mysqli_escape_string($this->startMysql(),$string));
    }

    public static function rs($msg,$jump=''){
        $arr['status']=true;
        $arr['msg']=$msg;
        $arr['jump']=$jump;
        echo json_encode($arr);
        die();
    }

    public static function rf($msg,$jump=''){
        $arr['status']=false;
        $arr['msg']=$msg;
        $arr['jump']=$jump;
        echo json_encode($arr);
        die();
    }

    private function session_is_started() {
        if (php_sapi_name() !== 'cli') {
            if (version_compare(phpversion(), '5.4.0', '>=')) {
                return session_status() === PHP_SESSION_ACTIVE ? true : false;
            } else {
                return session_id() === '' ? false : true;
            }
        }
        return false;
    }

    private function session_start(){
        if ($this->session_is_started() === false) {
            session_start();
        }
    }

    public function session_restart($salt=''){
        if ($this->session_is_started() === true) {
            session_destroy();
        }

        session_id(date("YmdHis",strtotime("now")).md5(base64_encode($salt.rand(0,999999999999))));
        session_start();
    }

    public function checkLogin(){
        if (self::$is_check_login){
            if (empty($_SESSION['stuid']))
                self::rf("请登录");
        }
    }


}